﻿using MengLin.OOP.DesignPattern.WMS_LC.Interface;
using MengLin.OOP.DesignPattern.WMS_LC.Interface.Observer;
using System.Collections.Generic;

namespace MengLin.OOP.DesignPattern.WMS_LC.Implements
{
    /// <summary>
    /// 被观察者(也叫主题)
    /// Nimbus1.0硬件平台温度传感器(高成本硬件的平台)
    /// </summary>
    public class Nimbus1_0TemperatureSensor : AbstractTemperatureSensor
    {
        /// <summary>
        /// 观察者集合
        /// </summary>
        private List<IObserver> _monitoringScreens;

        /// <summary>
        /// Ctor
        /// </summary>
        public Nimbus1_0TemperatureSensor()
        {
            _monitoringScreens = new List<IObserver>();
        }

        /// <summary>
        /// Nimbus1.0硬件平台读取温度
        /// </summary>
        /// <returns></returns>
        public override double Read()
        {
            double currentTemp = 36;//假设当前温度
            //配合版本2调度器
            //NotifyObservers(currentTemp);

            return currentTemp;
        }

        /// <summary>
        /// 通知每1个观察者显示温度
        /// </summary>
        /// <param name="obj"></param>
        public override void NotifyObservers(double temp)
        {
            foreach (var item in _monitoringScreens)
            {
                ((IObserver)item).Update("temperature|"+ temp);
            }
        }
       
        /// <summary>
        /// 注册/添加观察者
        /// </summary>
        /// <param name="obj"></param>
        public override void AddObserver(IObserver obj)
        {
            _monitoringScreens.Add(obj);
        }

        /// <summary>
        /// 移除/删除观察者
        /// </summary>
        /// <param name="obj"></param>
        public override void UnRegisterObserver(IObserver obj)
        {
            _monitoringScreens.Remove(obj);
        }
    }
}
